<template>
  <div class="address">
    <div class="addTop">
      <span @click="handleBack" class="iconfont">&#xe697;</span>
      <span>地址选择</span>
    </div>

    <div class="wrapper">
      <div class="title">我的收获地址</div>
      <div
        class="addInfo"
        @click="
          () => {
            handleGoBackAddress(item._id);
          }
        "
        v-for="item in addressData"
        :key="item._id"
      >
        <div class="addInfo__left">
          <div class="addInfo__left__first">
            <span class="name">{{ item.name }}</span>
            <span class="tel">{{ item.phone }}</span>
          </div>
          <div class="addInfo__left__second">
            {{ item.city }}{{ item.department }}{{ item.houseNumber }}
          </div>
        </div>
        <div class="addInfo__right">
          <span class="iconfont">&#xe6db;</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useRoute, useRouter } from "vue-router";
import { ref, reactive, computed } from "vue";
import { get } from "@/API/axios.js";
import router from "@/router";

const useAddressAPIeffect = () => {
  let addressData = ref([]);
  const getAddressAPI = async () => {
    const res = await get("/api/user/address");
    if (res.errno === 0) {
      addressData.value = res.data;
    }
  };

  return { addressData, getAddressAPI };
};

export default {
  name: "addressedit",
  setup() {
    //路由参数
    const router = useRouter();
    const route = useRoute();
    const path = route.query.path;

    //请求获取地址 '/api/user/address'
    const { addressData, getAddressAPI } = useAddressAPIeffect();
    getAddressAPI();

    //调转路由
    const handleGoBackAddress = (id) => {
      router.push(`${path}?addId=${id}`);
    };
    const handleBack = () => {
      router.back();
    };

    return { addressData, handleGoBackAddress, handleBack };
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.address {
  height: 100vh;
  width: 100%;
  background-color: $searchBgc-color;
}

.title {
  font-size: 0.14rem;
  line-height: 0.2rem;
  color: $content-color;
}

.addInfo {
  height: 1.05rem;
  width: 100%;
  border-radius: 0.04rem;
  background-color: $white-color;
  margin-top: 0.12rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0.18rem;
  &__left {
    width: 2.6rem;
    &__first {
      height: 0.2rem;
      font-size: 0.14rem;
      color: $light-font-color;
      line-height: 0.16rem;
      margin-bottom: 0.08rem;
      .name {
        margin-right: 0.8rem;
      }
    }
    &__second {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-color;
    }
  }
  &__right {
    font-size: 0.2rem;
    color: $light-font-color;
    transform: rotate(180deg);
  }
}

.addTop {
  box-sizing: border-box;
  height: 0.45rem;
  width: 100%;
  text-align: center;
  line-height: 0.45rem;
  font-size: 0.16rem;
  color: $content-color;
  background-color: $white-color;
  margin-bottom: 0.12rem;
  padding: 0 0.12rem;
  display: flex;
  justify-content: start;
  align-items: center;
  .iconfont {
    margin-right: 35%;
  }
  a {
    color: $content-color;
  }
}

.df {
  display: flex;
  justify-content: start;
  align-items: center;
  height: 0.45rem;
  background-color: $white-color;
  font-size: 0.15rem;
  color: $medium-font-color;
  line-height: 0.45rem;
  border-bottom: solid 0.01rem #f1f1f1;
  input {
    outline: none;
    border: none;
    margin-left: 0.06rem;
    color: $medium-font-color;
  }
  .inputPlaceholder {
    font-size: 0.14rem;
    color: $light-font-color;
    line-height: 0.2rem;
  }
}
</style>
